<?php

$CMS->class->date = new class_date;

class class_date {

	/**
	 * @param $today
	 */
	 
	public $today = "";
	
	/**
	 * @param $day
	 */
	 
	public $day = "";
	
	/**
	 * @param $month
	 */
	 
	public $month = "";
	
	/**
	 * @param $year
	 */
	 
	public $year = "";
	
	/**
	 * @param $theday
	 */
	 
	public $theday = "";
	
	/**
	 * @param $today
	 */
	 
	public $dayofweek = "";
	
	/**
	 * @param $now
	 */
	 
	public $now = "";
	
	/**
	 * @param $firstday
	 */
	
	public $firstday = "";
	
	//===========================================================================
	//  AUTO RUN
	//===========================================================================

	public function auto_run()
	{
		global $CMS, $DB;
	
		$str_search = array ( "Mon", 
					"Tue", 
					"Wed", 
					"Thu", 
					"Fri", 
					"Sat", 
					"Sun",
					"am", 
					"pm",
					":" );
					
		$str_replace = array ("{$CMS->lang['monday']}", 
					"{$CMS->lang['tuesday']}", 
					"{$CMS->lang['wednesday']}",
					"{$CMS->lang['thursday']}", 
					"{$CMS->lang['friday']}",  
					"{$CMS->lang['saturday']}", 
					"{$CMS->lang['sunday']}", 
					"{$CMS->lang['am']}", 
					"{$CMS->lang['pm']}",
					":" );
		
		$realtime = time();

		$now = date("D, d/m/Y - h:i:s a", $realtime);
		$this->now = str_replace($str_search, $str_replace, $now) . " <small>(GMT{$CMS->vars['timezone']})</small>";
		$this->today = date("d/m/Y", $realtime);
		
		$this->dayofmonth = date("t", $realtime); // Return 28-31
		$this->dayofweek = date("N", $realtime);
		$this->day = date("d", $realtime);
		$this->month = date("m", $realtime);
		$this->year = date("Y", $realtime);
		$this->cycle = $this->day > 15 ? 2 : 1;
		$this->theday = date("w", $realtime);
		
		$CMS->vars['now'] = $this->now;
		$CMS->vars['today'] = $this->today;
		$CMS->vars['this_dayofweek'] = $this->dayofweek;
		$CMS->vars['this_day'] = $this->day;
		$CMS->vars['this_month'] = $this->month;
		$CMS->vars['this_year'] = $this->year;
		
		// Extends
		$this->firstday = date("N", $this->gmt( 1, $this->month, $this->year ));
	}

	public function date_format( $input, $type = 0 )
	{
		global $CMS, $DB;
		
		if ( $type == 1 )
		{
			$type = $CMS->vars['long_time'];
		}
		else if ( !ereg ("^[-]?[0-9]+([\.][0-9]+)?$", $type) )
		{
			$type = $type;
		}
		else
		{
			$type = $CMS->vars['short_time'];
		}

		$output = gmdate( $type, $input + $CMS->vars['timezone']*3600 );

		return $output;
	}
	
	public function gmt( $day, $month, $year )
	{
		global $CMS, $DB;

		$output = @mktime(0, 0, 0, $month, $day, $year ) + $CMS->vars['timezone']*3600;

		return $output;
	}
	
	public function date_get( $type, $time )
	{
		global $CMS;

		$data = date( $type, $time ); //  + $CMS->vars['timezone']*3600 
		
		return $data;
	}

	/**
	 * @param $input
	 *		Date to time, dd/mm/yyyy -> timestamp
	 */
	
	public function date2time( $input, $remove_timezone = 0 )
	{
		global $CMS;
		
		// Backup time zone
		if ( $remove_timezone == 1 )
		{
			$timezone = $CMS->vars['timezone'];
			$CMS->vars['timezone'] = 0;
		}
		
		// Convert
		$input = explode("/", $input);
		$output = $this->gmt($input[0], $input[1], $input[2]);
		
		// Restore time zone
		if ( $remove_timezone == 1 )
		{
			$CMS->vars['timezone'] = $timezone;
		}
		
		return $output;
	}

	public function clear_time( $input )
	{
		global $CMS;
	
		// Backup time zone
		//$timezone = $CMS->vars['timezone'];
		//$CMS->vars['timezone'] = 0;
		
		// Convert
		$input = $this->date_format( $input, 0 );
		$input = explode("/", $input);
		$output = $this->gmt($input[0], $input[1], $input[2]);
		
		// Restore time zone
		//$CMS->vars['timezone'] = $timezone;

		return $output;
	}
	
	public function count( $input )
	{
		$time = intval($input);
	
		// Days
		$days = floor($time / (3600*24));
		
		// Hours
		$hours = $time;
		
		if ( $days > 0 )
		{
			$hours = $hours - $days*3600*24;
		}
	
		$hours = floor($hours / 3600);
		
		// Minutes
		$minutes = $time;
		
		if ( $days > 0 )
		{
			$minutes = $minutes - $days*3600*24;
		}
		
		if ( $hours > 0 )
		{
			$minutes = $minutes - $hours*3600;
		}
		
		$minutes = floor($minutes / 60);
		
		// Minutes
		$seconds = $time;
		
		if ( $days > 0 )
		{
			$seconds = $seconds - $days*3600*24;
		}
		
		if ( $hours > 0 )
		{
			$seconds = $seconds - $hours*3600;
		}
		
		if ( $minutes > 0 )
		{
			$seconds = $seconds - $minutes*60;
		}
		
		$count = array();
		$count[0] = $days;
		$count[1] = $hours;
		$count[2] = $minutes;
		$count[3] = $seconds;
		$count["d"] = $days;
		$count["h"] = $hours;
		$count["m"] = $minutes;
		$count["s"] = $seconds;
		
		return $count;
	}
	
	public function add_month( $input, $month = 0 )
	{
		if ( ! $month )
		{
			return $input;
		}
		
		$end_cycle = $this->date_format( $input, 0 );
		$end_cycle = explode("/", $end_cycle);
			
		$end_day = $end_cycle[0];
		$end_month = $end_cycle[1] + $month;
	
		// Check the value if the month more than 12
		if ( $end_month > 12 )
		{
			$sodu = floor($end_month / 12);
			$end_month = $end_month - ($sodu * 12 );					
			$end_year = $end_cycle[2] + $sodu;
		}
		else
		{
			$end_year = $end_cycle[2];
		}
					
		// Return the month to 2 numbers (x -> xx, ex: 2 -> 02)
		$end_month = (strlen( $end_month ) < 2) ? "0".$end_month : $end_month;	
		
		$output = $this->gmt($end_day, $end_month, $end_year);
		
		return $output;
	}
	
	/**
	 * @param $input
	 *		Timestamp
	 */
	
	public function add_hour( $input, $hour = 0 )
	{
		$output = $input + ($hour * 3600);
		
		return $output;
	}
	
	/**
	 * @param $input
	 *		Timestamp
	 */
	
	public function add_minute( $input, $minute = 0 )
	{
		$output = $input + ($minute * 60);
		
		return $output;
	}
	
	/**
	 * @param $get_dayofmonth
	 *		Timestamp
	 */
	
	public function get_dayofmonth( $month, $year )
	{
		$output = date("t", mktime(0, 0, 0, $month, 1, $year));
		
		return $output;
	}
	
	/**
	 * @param $get_dayofweek
	 *		Timestamp
	 */
	
	public function get_dayofweek( $day, $month, $year )
	{
		$output = date("N", mktime(0, 0, 0, $month, $day, $year));
		
		return $output;
	}
	
	/****************************************************************************
	* FUNCTION : time_since
	* $time_start : time begin ( in UNIX time )
	* return format time like:  1 minute and 5 seconds ago
	****************************************************************************/
	public function time_since($time_start)
	{
		global $CMS, $DB, $member;
		
		$now = time();
		$diff = $now - $time_start;
		switch (1) 
		{
			case ( $diff < 60 ):
				$count = $diff;
				if ( $count == 0 ) $count = $CMS->lang['moment'];
				else if ( $count == 1 ) $suffix = $CMS->lang['second'];
				else $suffix = $CMS->lang['seconds'];
			break;
			
			case ( $diff > 60 && $diff < 3600 ):
				$count = floor($diff/60);
				if ( $count == 1 ) $suffix = $CMS->lang['minute'];
				else $suffix = $CMS->lang['minutes'];
			break;
			
			case ( $diff > 3600 && $diff < 86400 ):
				$count = floor($diff/3600);
				if ( $count == 1 ) $suffix = $CMS->lang['hour'];
				else $suffix = $CMS->lang['hours'];
			break;
			
			case ( $diff > 86400 && $diff < 2629743 ):
				$count = floor($diff/86400);
				if ( $count == 1 ) $suffix = $CMS->lang['day'];
				else $suffix = $CMS->lang['days'];
			break;
			
			case ( $diff > 2629743 && $diff < 31556926 ):
				$count = floor($diff/2629743);
				if ( $count == 1 ) $suffix = $CMS->lang['month'];
				else $suffix = $CMS->lang['month'];
			break;
			
			case ( $diff > 31556926 ):
				$count = floor($diff/31556926);
				if ( $count == 1 ) $suffix = $CMS->lang['year'];
				else $suffix = $CMS->lang['years'];
			break;
		}
		
		return $count." ".$suffix.$CMS->lang['ago'];
	}
}

?>